/********************************************************************
    copyright (c) by tnmujoy
    @created:	10/19/2011   15:39
    @filename: 	CompositeDemo
    @author:	Limiko Lau (limiko.lau@gmail.com)
    @brief:	    
*********************************************************************/

#ifndef DEMO_COMPOSITEDEMO_H_
#define DEMO_COMPOSITEDEMO_H_

#include <string>
#include <iostream>
#include "DesignPattern/Composite.hxx"

class ComponentDemo : public tnmujoy::gdut::Component<ComponentDemo>
{
public:
    ComponentDemo()
    {

    }
    void test()
    {
        std::cout<<name_<<std::endl;
        std::cout<<"parent:"<<getParentsStr()<<std::endl;
        std::cout<<"children:"<<getChildrenStr()<<std::endl;
    }
    std::string getParentsStr()
    {
        std::string str;
        std::list<ComponentDemo*>::iterator itr = parents_.begin();
        for (; itr != parents_.end(); ++itr)
        {
            str += (*itr)->getName();
            str += " ";
        }
        return str;
    }
    virtual std::string getChildrenStr() = 0;
    std::string getName()
    {
        return name_;
    }
protected:
    std::string name_;
private:
};

class CompositeDemo : public tnmujoy::gdut::Composite<ComponentDemo>
{
public:
    CompositeDemo(std::string _name)
    {
        name_ = _name;
    }
    virtual std::string getChildrenStr()
    {
        std::string str;
        std::list<ComponentDemo*>::iterator itr = children_.begin();
        for (; itr != children_.end(); ++itr)
        {
            str += (*itr)->getName();
            str += " ";
        }
        return str;
    }
protected:
private:
};

class LeafDemo : public tnmujoy::gdut::Leaf<ComponentDemo>
{
public:
    LeafDemo(std::string _name)
    {
        name_ = _name;
    }
    virtual std::string getChildrenStr()
    {
        return "";
    }
protected:
private:
};

#endif //DEMO_COMPOSITEDEMO_H_

//
//History:
//[10/19/2011] <limiko.lau@gmail.com> created